package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public CaptchaVo createCaptchaVo() {
            //，准备一个KEY
        String key= RedisConstant.ADMIN_LOGIN_PREFIX+ UUID.randomUUID();
            //准备验证码
        SpecCaptcha specCaptcha=new SpecCaptcha(130,48,4);
        specCaptcha.setCharType(Captcha.TYPE_ONLY_NUMBER);
        //在toBase64()之前先获取code
        String code = specCaptcha.text();
        String img = specCaptcha.toBase64() ;
                // 将验证码响应给客户端同事保存到redis中
        redisTemplate.opsForValue().set(key,code,RedisConstant.ADMIN_LOGIN_CAPTCHA_TTL_SEC,TimeUnit.SECONDS);


        CaptchaVo captchaVo=new CaptchaVo(img,key);


        return captchaVo;
    }
}
